#include "client.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "scan.h"
#include "button.h"
#include "page.h"
#include "smart_config.h"
#include "esp_event.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lvgl.h"
#include "led.h"

#define HOST_IP_ADDR "192.168.0.126"
#define PORT            8600
static const char *TAG = "socket";
int sock = -1;
void client_thread(void *p)
{
    char rx_buffer[128];
    char host_ip[] = HOST_IP_ADDR;
    int addr_family = 0;
    int ip_protocol = 0;

    while (1)
    {
        struct sockaddr_in dest_addr;
        dest_addr.sin_addr.s_addr = inet_addr(host_ip);
        dest_addr.sin_family = AF_INET;
        dest_addr.sin_port = htons(PORT);
        addr_family = AF_INET;
        ip_protocol = IPPROTO_IP;

        sock =  socket(addr_family, SOCK_STREAM, ip_protocol);
        if (sock < 0)
        {
            ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
            goto reconn;
        }
        ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT);

        int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
        if (err != 0)
        {
            ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
            goto reconn;
        }
        ESP_LOGI(TAG, "Successfully connected");

        while (1)
        {

            int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
            // Error occurred during receiving
            if (len < 0)
            {
                ESP_LOGE(TAG, "recv failed: errno %d", errno);
                goto reconn;
            }
            // Data received
            else
            {
                rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
                if (len >= 6)
                {
                    if (rx_buffer[0] == 0xa5 && rx_buffer[1] == 0xa5)
                    {
                        if (rx_buffer[2] == 0x01)
                        {
                            set_led_num(rx_buffer[3], rx_buffer[4], rx_buffer[5]);
                        }
                    }
                }

            }
        }
reconn:
        if (sock >= 0)
        {
            ESP_LOGE(TAG, "Shutting down socket and restarting...");
            shutdown(sock, 0);
            close(sock);
        }
        vTaskDelay(5000 / portTICK_PERIOD_MS);
    }
    vTaskDelete(NULL);
}
void heart_thread(void *P)
{
    unsigned char str[6] = {0x5a, 0x5a, 0x01};
    if (sock >= 0 && bar_r.bar != 0)
    {
        str[3] = lv_bar_get_value(bar_r.bar);
        str[4] = lv_bar_get_value(bar_g.bar);
        str[5] = lv_bar_get_value(bar_b.bar);
        send(sock, str, 6, 0);
    }
}
void client_init()
{
    xTaskCreate(client_thread, "client", 4096, NULL, 3, NULL);
    lv_timer_t * task = lv_timer_create(&heart_thread,5000, 0);
}